package de.gameduell.framework.asset {

	import de.gameduell.framework.asset.Asset;
	import de.gameduell.framework.debug.Debug;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;	

	/**
	 * @author GD
	 */
	public class BinaryAsset extends Asset {

		protected var _loader:Loader;
		
		public function BinaryAsset(aId:String, aUrl:String, useSameDomain:Boolean = true) {
			super(aId, aUrl, useSameDomain);
			_type  = AssetTypes.BINARY;
			_loader = new Loader();
			registerLoader(_loader.contentLoaderInfo);
		}
		
		override public function get percentLoaded():int{
			return Math.round((_loader.contentLoaderInfo.bytesLoaded/ _loader.contentLoaderInfo.bytesTotal )*100);
		}
			
		override public function get data():Object{
			return _loader.content;
		}
		
		public function get applicationDomain():ApplicationDomain{
			try{
				var appDomain:ApplicationDomain = _loader.content.loaderInfo.applicationDomain;
			}catch(e:Error){
				Debug.trace("get applicationDomain "+e.getStackTrace());
			}
			return appDomain;	
		}
		
		override public function load():void {
			if(_useSameDomain) {
				var context:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
				context.checkPolicyFile   = true;
				_loader.load(new URLRequest(url), context);
			}else{
				_loader.load(new URLRequest(url));
			}
		}
		
//		override public function get loader():Loader{
//			return _loader;
//		}
	}
}
